#include <iostream>
/**
 * 1. 提供一个工厂类： 产生不同的产品
 * 2. 提供一个抽象产品类： 运算符 + 负责运算，得到结果
 * 3. 提供一个具体产品类：具体运算符
 */

// 抽象产品类
class Operation {
public:
    virtual double GetResult() = 0;

protected:
    double m_leftValue { 1.0 };
    double m_rightValue { 2.0 };
};

//具体产品
class AddOperation : public Operation {
public:
    double GetResult() override {
        return m_leftValue + m_rightValue;
    }
};

class SubOperation : public Operation {
public:
    double GetResult() override {
        return m_leftValue - m_rightValue;
    }
};

class MulOperation : public Operation {
public:
    double GetResult() override {
        return m_leftValue * m_rightValue;
    }
};

class DivOperation : public Operation {
public:
    double GetResult() override {
        return m_leftValue / m_rightValue;
    }
};

//工厂类，负责整个产品的创建
class OperatorFactory {
public:
    OperatorFactory() = default;
    ~OperatorFactory() = default;

    static Operation* createOperation(char operation) {
        switch (operation) {
        case '+':
            return new AddOperation; 
        case '-':
            return new SubOperation;
        case '*':
            return new MulOperation;
        case '/':
            return new DivOperation;
        default:
            break;
        }
        return nullptr;
    }
};

int main(int argc, char const *argv[]) {
    Operation* add = OperatorFactory::createOperation('+');
    std::cout << "add->getResult(): " << add->GetResult() << "\n";
    Operation* sub = OperatorFactory::createOperation('-');
    std::cout << "sub->getResult(): " << sub->GetResult() << "\n";
    Operation* mul = OperatorFactory::createOperation('*');
    std::cout << "mul->getResult(): " << mul->GetResult() << "\n";
    Operation* div = OperatorFactory::createOperation('/');
    std::cout << "div->getResult(): " << div->GetResult() << "\n";
    return 0;
}